﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BookiFramework.Helpers
{
    public static class UsingHelper
    {
        public static void Using<T>(Func<T> builder,Action<T> action)where T:IDisposable
        {
            using (var ctx = builder())
            {
                action(ctx);
            }
        }

        public static void Using<T>(Action<T> action) where T : IDisposable,new()
        {
            using (var ctx = new T())
            {
                action(ctx);
            }
        }

        public static T2 Using<T,T2>(Func<T> builder, Func<T,T2> action) where T : IDisposable
        {
            using (var ctx = builder())
            {
                return action(ctx);
            }
        }

        public static T2 Using<T,T2>(Func<T,T2> action) where T : IDisposable, new()
        {
            using (var ctx = new T())
            {
                return action(ctx);
            }
        }
    }
}
